package com.liusand.admin.controller;


import com.liusand.admin.service.BlogService;
import com.liusand.xo.common.Result;
import com.liusand.xo.pojo.Blog;
import com.liusand.xo.vo.BlogVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RequestMapping("blog")
@RestController
public class BlogController {
    @Autowired
    private BlogService blogService;

    @PostMapping("add")
    public Result add(@RequestBody Blog blog){
        if(blog.getTitle() == null || blog.getTitle().trim().length() == 0){
            return Result.error("标题不能为空");
        }
        if(blog.getTitle().length() > 50){
            return Result.error("标题过长");
        }
        if (blog.getCategoryId() == null || blog.getCategoryId().trim().length() == 0) {
            return Result.error("分类ID不能为空");
        }
        if (blog.getAuthor() == null || blog.getAuthor().trim().length() == 0) {
            return Result.error("作者不能为空");
        }
        if (blog.getAuthor().length() > 10) {
            return Result.error("作者名过长");
        }
        if(blog.getContent() == null || blog.getContent().length() == 0){
            return Result.error("内容不能为空");
        }
        boolean result = blogService.add(blog);
        if(!result){
            return Result.error();
        }
        return Result.success();
    }

    @GetMapping("findAll")
    public Result findAll(){
        List<BlogVO> lists = blogService.findAll();
        return Result.success(lists);
    }

    @GetMapping("findById/{id}")
    public Result findById(@PathVariable String id){
        if(id == null || id.length() == 0){
            return Result.error();
        }
        BlogVO vo = blogService.findVOById(id);
        if(vo == null) {
            return Result.error("查询失败");
        }
        return Result.success(vo);
    }

    @GetMapping("findPojoById/{id}")
    public Result findPojoById(@PathVariable String id){
        if(id == null || id.length() == 0){
            return Result.error();
        }
        Blog blog = blogService.findById(id);
        if(blog == null) {
            return Result.error("查询失败");
        }
        return Result.success(blog);
    }

    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable String id){
        if(id == null || id.length() == 0){
            return Result.error();
        }
        boolean result = blogService.deleteById(id);
        if(!result) {
            return Result.error("查询失败");
        }
        return Result.success();
    }

    @PutMapping("update")
    public Result update(@RequestBody Blog blog){
        if(blog.getTitle() != null && blog.getTitle().trim().length() == 0){
            return Result.error("标题不能为空字符串");
        }
        if(blog.getTitle() != null && blog.getTitle().length() > 50){
            return Result.error("标题过长");
        }
        if (blog.getAuthor() != null && blog.getAuthor().trim().length() == 0) {
            return Result.error("作者不能为空字符串");
        }
        if (blog.getAuthor() != null && blog.getAuthor().length() > 10) {

            return Result.error("作者名过长");
        }
        boolean result = blogService.update(blog);
        if(!result){
            return Result.error("更新失败");
        }
        return Result.success();
    }

}
